const app = getApp(), util = require("../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    MapHeight: "100vh",
    location: [],
    banner:[],
    links:[
      { pic: '../../images/index_img/44.png',event:'toPate' },
      { pic: '../../images/index_img/22.png',event:'toPay' },
      { pic: '../../images/index_img/11.png',event:'toRecharge' },
      { pic: '../../images/index_img/33.png',event:'toVip1' }
    ],
    backbtnY: 0,
    markers: [],
    indexData: [],
    indexItem: {}
    //config里改appid: 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      backbtnY: wx.getSystemInfoSync().windowHeight - 200
    })
  },

  toBannerLink(e){
    let url = e.currentTarget.dataset.name
    if (url) {
      wx.navigateTo({
        url: url,
      })
    }
  },
  toApp(){
    wx.showModal({
      title:"提示",
      content:"即将打开"+'"杭州便捷泊车"'+"小程序",
      confirmColor:"#0058FF",
      cancelColor:"#DB4141"
    })
  },
  toPate(){
    wx.navigateTo({
      url: '/pages/ucenter/addLicensePlate/index',
    })
  },

  toPay() {
    wx.switchTab({
      url: '/pages/play/index',
    })
  },

  toRecharge() {
    wx.navigateTo({
      url: '/pages/payDetail/index',
    })
  },

  getLocation() {
    var that = this;
    wx.getLocation({
      type: 'gcj02',
      success: function (res) {
        console.log(res);
        that.setData({
          location: [res.latitude, res.longitude],
          markers: [{
            latitude: res.latitude,//纬度
            longitude: res.longitude,//经度
            iconPath: "../../images/map.png", //位置图标
            width: 30,
            height: 36,
          },]
        })
        that.getNearby();
      },
      fail(res) {
        wx.showModal({
          title: '获取当前位置失败',
          content: '请确认授权获取地理位置信息，否则地图功能将无法使用',
          showCancel: false,
          success(res) {
            if (res.confirm) {
              wx.openSetting({
                success(e) {
                  console.log(e)
                  if (e.authSetting["scope.userLocation"]) {
                    that.onLoad()
                  }
                },
                fail(res) {
                  wx.showModal({
                    title: '错误提示',
                    content: '调用授权窗口失败，请检查您的手机设置',
                    showCancel: false,
                  })
                }
              })
            }
          }
        })
      }
    })
  },
  toVip1(){
    wx.navigateTo({
      url: '/pages/Apack/index',
    })
  },
  toVoucherPage(){
    wx.request({
      url: app.globalData.basicUrl+'user/notice/list',
      success:function(res){
        console.log(res.data.rows[0]);
        if(res.data.rows[0].isRead == 1){
          wx.navigateTo({
            url: '/pages/rVouncerInfo/index',
          })
        }else{
          wx.navigateTo({
            url: '/pages/voucher/index?currentDate=0',
          })
        }
        
      },
      fail(res){
        wx.hideLoading()
        wx.showToast({
          icon:'none',
          title: '获取失败！',
        })
      }
    })
   
  },
  // 获得附近停车场数据，并显示标记
  getNearby: function () {
    var that = this;
    var mk = this.data.markers;
    wx.request({
      url: app.globalData.basicUrl + 'system/parking/findAll',
      success: function (res) {
        res.data.data.map(item => {
          item.distance = Number(util.distance(that.data.markers[0].latitude, that.data.markers[0].longitude, item.parkingLatitude, item.parkingLongitude))
          var marker = {
            id: item.parkingId,
            latitude: item.parkingLatitude,//纬度
            longitude: item.parkingLongitude,//经度
            iconPath: "../../images/p.png", //附近停车场位置图标
            width: 30,
            height: 36,
            // callout: {
            //   iconPath: "../../images/map.png", //位置图标
            //   content: item.parkingName+" \n还剩"+item.parkingSpace+"个空车位",   //content: " 厦门思明区政府 \n 12000元/㎡",
            //   fontSize: 12,
            //   borderRadius: 5,
            //   borderWidth: 1,
            //   borderColor: "#ffffff",
            //   padding: 3,
            //   textAlign: "center",
            //   display: "BYCLICK"
            // }
          }
          // console.log('that.markers', that.data.markers);
          mk.push(marker);
        })
        that.setData({
          markers: mk,
          indexData: res.data.data.sort(that.compare("distance", true)),
          indexItem: {}
        });
      }
    })
  },

  getIndexBanner(){
    var that=this;
    wx.request({
      url: app.globalData.basicUrl + 'system/banner/list',
      data:{
        bannerStatus:0
      },
      success(res){
        if (res.statusCode == 200) {
          that.setData({
            banner:res.data.rows
          })
        }
      }
    })
  },

  SystemNotification: function () {
    wx.navigateTo({
      url: '/pages/SystemNotification/index',
    })
  },

  compare(property, desc) {
    return util.compare(property, desc)
  },

  /**
   * 回到自己位置，在cover-image上绑定点击事件即可.
   */
  clickcontrol(e) {
    console.log(e);
    this.onShow()
  },

  getCoordinate(e) {
    // map是id
    let that = this, markers = this.data.markers;
    const mpCtx = wx.createMapContext("map");
    console.log(e, '该点信息')
    function moveToLoc(lat, long) {
      markers[0] = {
        latitude: lat,//纬度
        longitude: long,//经度
        iconPath: "../../images/map.png", //位置图标
        width: 30,
        height: 36,
      }
      that.setData({
        markers: markers
      })
      that.getNearby();
    }
    if (e.detail.latitude) {
      moveToLoc(e.detail.latitude, e.detail.longitude)
    } else if (mpCtx.getRegion) {
      mpCtx.getRegion({
        success(res) {
          moveToLoc((res.northeast.latitude + res.southwest.latitude) / 2, (res.northeast.longitude + res.southwest.longitude) / 2)
        }
      })
    }
  },

  toLocation(e) {
    console.log(e, '停车点信息')
    var that = this, indexData = this.data.indexData;
    indexData.map(item => {
      item.distance = Number(util.distance(that.data.markers[0].latitude, that.data.markers[0].longitude, item.parkingLatitude, item.parkingLongitude))
      if (e.markerId == item.parkingId) {
        console.log(item, '停车点')
        this.setData({
          indexItem: item
        })
      }
    })
  },

  SystemNotification: function () {
    wx.navigateTo({
      url: '/pages/SystemNotification/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getLocation()
    this.getIndexBanner()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
